package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;




@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
     private  EmployeeService employeeService;
     // 登录  过滤器筛选路径

    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){


        //1、 将页面提交的密码password进行md5加密处理
        String username = employee.getUsername();
        String password = employee.getPassword();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
         //eq(字段名,字段值)
        queryWrapper.eq(Employee :: getUsername,username);
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        Employee emp = employeeService.getOne(queryWrapper);


        //3、如果没有查询到则返回用户名不正确
         if(emp == null){
             return R.error("用户名不正确!");
         }
        //4、密码比对，如果不一致则返回密码错误
         if(!StringUtils.equals(password,emp.getPassword())){
             return R.error("密码错误!");
         }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
         if(emp.getStatus()==0){
             return R.error("员工状态为被禁用!");
         }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        HttpSession session = request.getSession();
         session.setAttribute("employee",emp.getId());

         return R.success(emp);

    }

  @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功!");
  }

   //增加成员   唯一主键值重复,全局构造器处理异常
  @PostMapping
  public R save(@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
      employeeService.save(employee);
      return R.success("增加成功!");
  }

   //分页查询  配合分页插件使用
  @GetMapping ("page")
    public R  page(Integer page,Integer pageSize,String name){
      log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
      //创建分页构造器
      Page<Employee> pageInfo = new Page(page, pageSize);
      //创建条件构造器
      LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
      //添加查询条件
      queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name).orderByDesc(Employee ::getUpdateTime);
      //执行查询
      employeeService.page(pageInfo,queryWrapper);

      return R.success(pageInfo);

  }
    /**
     * 根据id修改员工信息
     * 但是前端页面js处理数字数据容易出现精度缺失的情况,在WebMvcConfig中添加转换器
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        log.info(employee.toString());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据id查询员工信息
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}

